#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
集团预测算法演示脚本
展示加权复合增长率模型的计算过程
"""

def demonstrate_weighted_cagr_model():
    """演示加权复合增长率模型"""
    print("🧮 集团预测算法演示 - 加权复合增长率模型")
    print("=" * 60)
    
    # 模拟四个板块的数据
    sectors = {
        "人药制剂板块": {
            "historical_cagr": 0.06,      # 历史6%增长
            "industry_cagr": 0.08,        # 行业8%增长
            "market_stage_cagr": 0.07,    # 市场阶段7%增长
            "strategic_growth": 0.085,    # 战略目标8.5%增长
            "base_sales_revenue": 120.5,  # 基准年销售收入(亿元)
            "base_net_profit": 15.2       # 基准年净利润(亿元)
        },
        "人药原料板块": {
            "historical_cagr": 0.04,
            "industry_cagr": 0.05,
            "market_stage_cagr": 0.06,
            "strategic_growth": 0.06,
            "base_sales_revenue": 85.3,
            "base_net_profit": 12.8
        },
        "植保板块": {
            "historical_cagr": 0.12,
            "industry_cagr": 0.10,
            "market_stage_cagr": 0.11,
            "strategic_growth": 0.12,
            "base_sales_revenue": 45.7,
            "base_net_profit": 8.9
        },
        "动保板块": {
            "historical_cagr": 0.18,
            "industry_cagr": 0.15,
            "market_stage_cagr": 0.16,
            "strategic_growth": 0.15,
            "base_sales_revenue": 32.1,
            "base_net_profit": 6.4
        }
    }
    
    print("📊 各板块基准数据:")
    print("-" * 60)
    for sector_name, data in sectors.items():
        print(f"{sector_name}:")
        print(f"  📈 历史CAGR: {data['historical_cagr']*100:.1f}%")
        print(f"  🏭 行业CAGR: {data['industry_cagr']*100:.1f}%")
        print(f"  📊 市场阶段CAGR: {data['market_stage_cagr']*100:.1f}%")
        print(f"  🎯 战略增长目标: {data['strategic_growth']*100:.1f}%")
        print(f"  💰 基准销售收入: {data['base_sales_revenue']:.1f}亿元")
        print(f"  💵 基准净利润: {data['base_net_profit']:.1f}亿元")
        print()
    
    # 计算各板块的综合增长率
    print("🧮 综合增长率计算 (加权平均公式):")
    print("g = (g1 × 50%) + (g2 × 15%) + (g3 × 25%) + (g4 × 10%)")
    print("-" * 60)
    
    sector_comprehensive_rates = {}
    
    for sector_name, data in sectors.items():
        g1 = data['industry_cagr']        # 行业平均增速 (权重50%)
        g2 = data['historical_cagr']      # 历史增速 (权重15%)
        g3 = data['market_stage_cagr']    # 市场阶段预期 (权重25%)
        g4 = data['strategic_growth']     # 集团战略目标 (权重10%)
        
        # 加权平均计算
        comprehensive_rate = (g1 * 0.5) + (g2 * 0.15) + (g3 * 0.25) + (g4 * 0.1)
        sector_comprehensive_rates[sector_name] = comprehensive_rate
        
        print(f"{sector_name}:")
        print(f"  g1 (行业): {g1*100:.1f}% × 50% = {g1*0.5*100:.2f}%")
        print(f"  g2 (历史): {g2*100:.1f}% × 15% = {g2*0.15*100:.2f}%")
        print(f"  g3 (市场): {g3*100:.1f}% × 25% = {g3*0.25*100:.2f}%")
        print(f"  g4 (战略): {g4*100:.1f}% × 10% = {g4*0.1*100:.2f}%")
        print(f"  🎯 综合增长率: {comprehensive_rate*100:.2f}%")
        print()
    
    # 预测未来3年的数据
    prediction_years = [2025, 2026, 2027]
    base_year = 2024
    
    print("📈 未来3年预测结果:")
    print("-" * 60)
    
    group_totals = {year: {"sales": 0, "profit": 0} for year in prediction_years}
    
    for sector_name, data in sectors.items():
        comprehensive_rate = sector_comprehensive_rates[sector_name]
        base_sales = data['base_sales_revenue']
        base_profit = data['base_net_profit']
        profit_margin = base_profit / base_sales
        
        print(f"{sector_name}:")
        for year in prediction_years:
            years_ahead = year - base_year
            predicted_sales = base_sales * ((1 + comprehensive_rate) ** years_ahead)
            predicted_profit = predicted_sales * profit_margin
            
            group_totals[year]["sales"] += predicted_sales
            group_totals[year]["profit"] += predicted_profit
            
            print(f"  {year}年: 销售收入 {predicted_sales:.1f}亿元, 净利润 {predicted_profit:.1f}亿元")
        print()
    
    # 集团合计
    print("🏢 集团合计预测:")
    print("-" * 60)
    for year in prediction_years:
        total_sales = group_totals[year]["sales"]
        total_profit = group_totals[year]["profit"]
        group_margin = total_profit / total_sales * 100
        
        print(f"{year}年:")
        print(f"  📊 销售收入合计: {total_sales:.1f}亿元")
        print(f"  💰 净利润合计: {total_profit:.1f}亿元")
        print(f"  📈 净利润率: {group_margin:.2f}%")
        print()
    
    # 增长率分析
    print("📊 年度增长率分析:")
    print("-" * 60)
    
    base_total_sales = sum(data['base_sales_revenue'] for data in sectors.values())
    base_total_profit = sum(data['base_net_profit'] for data in sectors.values())
    
    print(f"基准年({base_year}):")
    print(f"  销售收入: {base_total_sales:.1f}亿元")
    print(f"  净利润: {base_total_profit:.1f}亿元")
    print()
    
    for year in prediction_years:
        sales_growth = (group_totals[year]["sales"] / base_total_sales - 1) * 100
        profit_growth = (group_totals[year]["profit"] / base_total_profit - 1) * 100
        
        print(f"{year}年相比基准年增长:")
        print(f"  📈 销售收入增长: {sales_growth:.2f}%")
        print(f"  💰 净利润增长: {profit_growth:.2f}%")
        print()

def main():
    """主演示函数"""
    print("🎯 齐鲁制药集团预测算法演示")
    print("基于历史趋势的加权复合增长率模型")
    print("=" * 60)
    print()
    
    demonstrate_weighted_cagr_model()
    
    print("✅ 演示完成!")
    print("=" * 60)
    print("💡 算法说明:")
    print("1. 采用四因素加权模型，权重分别为:")
    print("   - 行业平均增速: 50%")
    print("   - 历史增速: 15%")
    print("   - 市场阶段预期: 25%")
    print("   - 集团战略目标: 10%")
    print("2. 基于复合年均增长率进行线性外推")
    print("3. 保持各板块净利润率相对稳定")
    print("4. 最终汇总得到集团整体预测")

if __name__ == "__main__":
    main()
